package com.app.util;

public class IDCardValidator {

    public static boolean isValidIDCard(String id) {
        if (id == null) {
            return false;
        }
        id = id.trim();
        int length = id.length();
        if (length == 15) {
            return validate15DigitID(id);
        } else if (length == 18) {
            return validate18DigitID(id);
        } else {
            return false;
        }
    }

    private static boolean validate15DigitID(String id) {
        // 检查是否全为数字
        if (!id.matches("\\d+")) {
            return false;
        }
        String birthdayPart = id.substring(6, 12);
        String yy = birthdayPart.substring(0, 2);
        String mm = birthdayPart.substring(2, 4);
        String dd = birthdayPart.substring(4, 6);
        int year = 1900 + Integer.parseInt(yy);
        int month = Integer.parseInt(mm);
        int day = Integer.parseInt(dd);
        return isValidDate(year, month, day);
    }

    private static boolean validate18DigitID(String id) {
        // 检查格式
        if (!id.matches("\\d{17}[\\dXx]")) {
            return false;
        }
        // 验证生日
        String birthdayPart = id.substring(6, 14);
        int year = Integer.parseInt(birthdayPart.substring(0, 4));
        int month = Integer.parseInt(birthdayPart.substring(4, 6));
        int day = Integer.parseInt(birthdayPart.substring(6, 8));
        if (!isValidDate(year, month, day)) {
            return false;
        }
        // 计算并验证校验码
        char expectedCheckCode = calculateCheckCode(id);
        char actualCheckCode = Character.toUpperCase(id.charAt(17));
        return actualCheckCode == expectedCheckCode;
    }

    private static char calculateCheckCode(String id) {
        int[] weight = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
        char[] chars = id.toCharArray();
        int sum = 0;
        for (int i = 0; i < 17; i++) {
            sum += (chars[i] - '0') * weight[i];
        }
        int remainder = sum % 11;
        char[] checkCodes = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
        return checkCodes[remainder];
    }

    private static boolean isValidDate(int year, int month, int day) {
        if (month < 1 || month > 12) {
            return false;
        }
        int[] maxDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (month == 2 && isLeapYear(year)) {
            maxDays[1] = 29;
        }
        return day >= 1 && day <= maxDays[month - 1];
    }

    private static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }

    // 测试用例
    public static void main(String[] args) {
        System.out.println(isValidIDCard("110101890101123"));   // 15位有效
        System.out.println(isValidIDCard("110101900229123"));   // 15位无效日期
        System.out.println(isValidIDCard("11010519491231002X")); // 18位有效
        System.out.println(isValidIDCard("622101199507070711")); // 18位无效校验码
        System.err.println(isValidIDCard("622102199812193829"));
    }
}